// pages/login/phonelogin/phonelogin.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    timeout: false,
    seconds: 60,
    mobile: '',
    code: '',
    photo: '',
    name: '',
    sex: '',
    id: '',
    sid: ''
  },
  checkMobile: function(e) {
    this.setData({
      mobile: e.detail.value
    })
  },
  checkcode: function(e) {
    this.setData({
      code: e.detail.value
    })
  },
  sendVerifyCode: function(e) {
    let that = this;
    let mobile = this.data.mobile
    console.log(mobile)
    wx.request({
      url: app.globalData.api + "mobilesend",
      data: {
        phone: mobile
      },
      success: function(res) {
        console.log(res)
      }
    })

    this.setData({
      timeout: true
    })
    let timerId = setInterval(function() {
      var seconds = that.data.seconds - 1;
      that.setData({
        seconds: seconds
      })
      if (seconds <= 0) {
        clearInterval(timerId)
        that.setData({
          timeout: false,
          seconds: 60
        })
      }
    }, 1000);
  },
  // 登录按钮
  formSubmit() {
    let that = this
    if (!that.data.code || !that.data.mobile) {
      wx.showToast({
        title: '填写手机和验证码',
        icon: 'none'
      })
    } else {
      // 校验验证码
      wx.request({
        url: app.globalData.api + "vcode",
        data: {
          vcode: that.data.code,
          phone: that.data.mobile,
        },
        success: function(res) {
          console.log(res)
          if (res.data.code == 2) {
            wx.showToast({
              title: '验证码错误',
              icon: 'none'
            })
          } else {
            // 注册
            wx.request({
              url: app.globalData.api + "register",
              data: {
                phone: that.data.mobile,
                nickname: that.data.name,
                openid: that.data.id,
                avatarurl: that.data.photo,
                nickname: that.data.name,
                sex: that.data.sex,
                sid: that.data.sid
              },
              header: {
                "Content-Type": "application/x-www-form-urlencoded"
              },
              method: 'POST',
              success: function(res) {
                console.log(res)
                if (res.data.code == 0) {
                  wx.setStorage({
                    key: 'userinfo',
                    data: res.data.list,
                  })
                  wx.switchTab({
                    url: '/pages/index/index',
                  })
                } else {
                  wx.showToast({
                    title: res.data.msg,
                    icon: 'none'
                  })
                }
              }
            })
          }

        },
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options)
    let sid = ''
    if (options.sid > 0) {
      sid = options.sid
    } else {
      sid = 0
    }
    this.setData({
      name: options.nickname,
      photo: options.avatarurl,
      sex: options.gender,
      id: options.id,
      sid: sid
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})